package es.unknown.group.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import es.unknown.group.shared.GroupBean;
import es.unknown.group.shared.GroupsBean;

public class PnlGroup extends DockPanel {
	private StackPanel pnlGroups;

	public PnlGroup() {
		pnlGroups = new StackPanel();
		add(pnlGroups, DockPanel.WEST);
		loadGroups(pnlGroups);
	}

	// private final SvcGroupAsync svcGroup = GWT.create( SvcGroup.class);
	 public JsonpRequestBuilder jsonp = new JsonpRequestBuilder();

	private void loadGroups(StackPanel pnlGroups2) 
	{
		AsyncCallback<JavaScriptObject> asyncCallback_test = new AsyncCallback<JavaScriptObject>() {
			@Override
			public void onSuccess(JavaScriptObject groups) {
				GWT.log("OK:" + groups);
				Window.alert("OK:" + groups);
				// for( int i=0 ; i< groups.getGroups().length() ; i++) {
				// GroupBean group = groups.getGroups().get(i);
				//
				// VerticalPanel pnlGroup = new VerticalPanel();
				// for( final GroupBean subgroup : group.getSubgroups()) {
				// Label lbl = new Label(subgroup.getName());
				// pnlGroup.add( lbl);
				// lbl.addClickHandler(new ClickHandler(){
				// public void onClick(ClickEvent event) {
				// Window.alert("Hello " + subgroup.getName() + ", GWT World!");
				// }
				// });
				// }
				// }
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Orror:" + caught);
			}
		};

		AsyncCallback<GroupsBean> asyncCallback = new AsyncCallback<GroupsBean>() {
			@Override
			public void onSuccess(GroupsBean groups) {
				GWT.log("OK:" + groups);
				//Window.alert("OK:" + groups.getCode() + "-" + groups.getDesc() + "-" + groups.getGroups().length());
				for (int i = 0; i < groups.getGroups().length(); i++) {
					final GroupBean group = groups.getGroups().get(i);
					GWT.log("OK:" + group.getName());

					VerticalPanel pnlGroup = new VerticalPanel();
					Label lbl = new Label( group.getName());
					pnlGroup.add(lbl);
					lbl.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							Window.alert("Hello " + group.getName()
									+ ", GWT World!");
						}
					});
//					for (int j = 0; j < group.getSubgroups().length(); j++) {
//						final GroupBean subgroup = group.getSubgroups().get( j);
//						Label lbl = new Label(subgroup.getName());
//						GWT.log("listo 1");
//						pnlGroup.add(lbl);
//						lbl.addClickHandler(new ClickHandler() {
//							public void onClick(ClickEvent event) {
//								Window.alert("Hello " + subgroup.getName()
//										+ ", GWT World!");
//							}
//						});
//					}

					pnlGroups.add( pnlGroup, group.getName());
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Orror:" + caught);
			}
		};

		String url = "http://127.0.0.1:8080/processJSON?cmd={\"QueryGroups\":\"grumo-test\"}";
		// jsonp.setCallbackParam("callback");
		jsonp.requestObject(url, asyncCallback);

		// RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
		// URL.encode(url));
		//
		// try {
		// String current = "2011/07/13";
		// builder.sendRequest(current, new RequestCallback() {
		// public void onError(Request request, Throwable exception) {
		// Window.alert("Orror:" + exception);
		// }
		//
		// public void onResponseReceived(Request request,
		// Response response) {
		//
		// String responseText = response.getText();
		// try {
		// JSONValue jsonValue = JSONParser.parse( responseText);
		// // processJSON(jsonValue);
		// } catch (Exception e) {
		// Window.alert("Error1: " + e);
		// }
		// }
		// });
		// } catch (RequestException ex) {
		// Window.alert("Error2: " + ex);
		// }

	}

}
